<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/20
 * Time: 12:39
 */

namespace App\CronTask;

use EasySwoole\Component\Pool\Exception\PoolEmpty;
use EasySwoole\Component\Pool\Exception\PoolException;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\HttpClient\HttpClient;

class CsgoBattleCron extends AbstractCronTask
{
    public static function getRule(): string
    {
        // TODO: Implement getRule() method.
        //return '@daily';
        return '*/1 * * * *';
    }
    public static function getTaskName(): string
    {
        // TODO: Implement getTaskName() method.
        return 'CsgoBattle';
    }
    static function run(\swoole_server $server, int $taskId, int $fromWorkerId, $flags = null)
    {
        // TODO: Implement run() method.
        try {
            MysqlPool::invoke(function (MysqlObject $db) {
                $matchs = $db->where('status', 2)->orderBy('match_time', 'desc')->get('siam_csgomatchs', null, 'map_names,match_id');
                foreach ($matchs as $match) {
                    if ($match['map_names']) {
                        $map_names = json_decode($match['map_names'], true);
                        foreach ($map_names as $map_name) {
                            list($s1, $s2) = explode(' ', microtime());

                            if (!strstr($map_name, 'de_')) {
                                $map_name = strtolower('de_' . $map_name);
                            }

                            $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                            $liveBattle = $db->where('map_name', $map_name)->where('match_id', $match['match_id'])->getOne('siam_csgolive_battle');
                            if (!$liveBattle) {
                                $url = UrlEnums::FJURL . UrlEnums::CSGO_LIVE_BATTLE_PATH . "?match_id={$match['match_id']}&map_name=" . $map_name;
                                $client = new HttpClient($url);
                                $option = [
                                    'path' => UrlEnums::CSGO_LIVE_BATTLE_PATH,
                                    'api_time' => $microtime
                                ];
                                $header = Auth::getInstance()->header($option);
                                $client->setHeaders($header, true, false);
                                $response = $client->get();
                                $content = $response->getBody();
                                $data = json_decode($content, true);
                                if (isset($data['code']) && $data['code'] == 200 && $data['data']) {
                                    $data['data']['counter_terrorist_players'] = json_encode($data['data']['counter_terrorist_players']);
                                    $data['data']['terrorist_players'] = json_encode($data['data']['terrorist_players']);
                                    $data['data']['counter_terrorist_round_history_first_half'] = json_encode($data['data']['counter_terrorist_round_history_first_half']);
                                    $data['data']['counter_terrorist_round_history_second_half'] = json_encode($data['data']['counter_terrorist_round_history_second_half']);
                                    $data['data']['terrorist_round_history_first_half'] = json_encode($data['data']['terrorist_round_history_first_half']);
                                    $data['data']['terrorist_round_history_second_half'] = json_encode($data['data']['terrorist_round_history_second_half']);
                                    $data['data']['is_frozen'] = $data['data']['is_frozen'] == true ? 1 : 0;
                                    $data['data']['is_bomb_planted'] = $data['data']['is_bomb_planted'] == true ? 1 : 0;
                                    unset($data['data']['is_live']);
                                    $raw = $db->insert('siam_csgolive_battle', $data['data']);
                                }
                            }
                        }

                    }
                }
            });
        } catch (PoolEmpty $e) {
        } catch (PoolException $e) {
        } catch (\Throwable $e) {
        }
    }
}